import requests  # 请求模块
from bs4 import BeautifulSoup  # 数据解析提取模块，需要提前使用pip下载”
import json
import pandas as pd
from matplotlib import pyplot as plt

# 解决中文和负号显示问题
plt.rcParams['font.sans-serif'] = ['SimHei']  # 运行配置参数中的字体（font）为黑体（SimHei）


# 显示柱状上的数值
def auto_label(rects):
    total_width, n = 1, 4
    for rect in rects:
        height = rect.get_height()
        plt.text(rect.get_x() + rect.get_width() / n - 0.1, height + 1000, '%s' % float(height))


# 创建爬取数据函数
def crawl_wiki_data():
    # 创建请求头
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                      'Chrome/67.0.3396.99 Safari/537.36'
    }
    url = 'https://m.gasgoo.com/qcxl/article/72658.html'
    try:
        response = requests.get(url, headers=headers, stream=True, verify=False)
        # 创建解析对象
        soup = BeautifulSoup(response.text, 'lxml')
        # 查找表单标签
        div = soup.find_all("div", attrs={"class": "cont"})
        return div[0].find_all("table")
    except json.JSONDecodeError as e:
        print(e)


# 解析数据并保存json
def parse_wiki_data(html_table):
    # 把table转化为BeautifulSoup对象
    soup = BeautifulSoup(str(html_table), 'lxml')
    # 获取表格元素中的所有行
    all_trs = soup.find_all('tr')
    # 存储参赛学员信息
    cars = []
    for tr in all_trs[1:]:
        car = {}
        all_tds = tr.find_all('td')
        # 排名
        car["order"] = all_tds[0].text
        # 车型
        car["name"] = str(all_tds[3].find('a').text).replace(" ", "").replace("\r\n", "")
        # 当月销量
        car["month_sale"] = all_tds[4].text
        # 年度销量
        car["year_sale"] = all_tds[5].text
        # 环比
        car["circle_rate"] = all_tds[7].text
        # 同比
        car["similar_rate"] = all_tds[9].text
        # 添加
        cars.append(car)

    # 序列化编码
    with open('car_sale.json', 'w+', encoding="UTF-8") as file:
        json.dump(cars, file, ensure_ascii=False, indent=2)


def draw_save_data():
    cars_json = pd.read_json("car_sale.json", encoding="UTF-8")
    cars_json_month_sale = cars_json["month_sale"].astype(int)
    cars_json_year_sale = cars_json["year_sale"].astype(int)

    cars_json["month_sale"] = cars_json_month_sale
    cars_json["year_sale"] = cars_json_year_sale

    print("月销量数据统计：\r\n", cars_json_month_sale.describe())
    print("年销量数据统计：\r\n", cars_json_year_sale.describe())

    cars_json_month = cars_json.copy()
    cars_json_month.sort_values(by='month_sale', inplace=True, ascending=False)
    cars_plt_month = plt.bar(cars_json_month['name'][0:10], cars_json_month['month_sale'][0:10])
    auto_label(cars_plt_month)
    plt.title(u'月销量排名统计', size=15, color='red')
    plt.xlabel('车型', size=12, color='blue')
    plt.ylabel(u'月销量/辆', size=12, color='blue')
    plt.savefig('月销量排名前十.jpg')
    plt.show()

    cars_json_year = cars_json.copy()
    cars_json_year.sort_values(by='year_sale', inplace=True, ascending=False)
    cars_plt_year = plt.bar(cars_json_year['name'][0:10], cars_json_year['year_sale'][0:10])
    auto_label(cars_plt_year)
    plt.title(u'年销量排名统计', size=15, color='red')
    plt.xlabel('车型', size=12, color='blue')
    plt.ylabel(u'年销量/辆', size=12, color='blue')
    plt.savefig('年销量排名前十.jpg')
    plt.show()


if __name__ == '__main__':
    crawl_table = crawl_wiki_data()
    parse_wiki_data(crawl_table)
    draw_save_data()
